package oj;

/**
 * 两数相加
 */
public class LC2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode prev=null;
        ListNode cur=null;
        int sum=0;
        while(l1!=null || l2!=null){
            int n1=l1==null?0:l1.val;
            int n2=l2==null?0:l2.val;
            if(prev==null){
                prev=cur=new ListNode((sum+n1+n2)%10);
            }else{
                cur.next=new ListNode((sum+n1+n2)%10);
                cur=cur.next;
            }
            sum=(sum+n1+n2)/10;
            if(l1!=null)  l1=l1.next;
            if(l2!=null)  l2=l2.next;
        }
        if(sum>0){
            cur.next=new ListNode(sum);
        }
        return prev;
    }
}
